AS2MineSweeper
AS2MineSweeper is an implementation of the classic Minesweeper game based in Macromedia Flash MX2004. The object of the game is to locate all the mines on a grid without uncovering any of them. AS2MineSweeper was programmed using ActionScript 2.0 and it utilizes the Model-View-Controller (MVC) design pattern. MVC is an effective way to cleanly separate data, user input handling, and presentation aspects of an application. The benefit of such an architecture is that changes to any one of these three distinct parts can be made largely without affecting the behavior of the others. In AS2MineSweeper, this aspect is demonstrated through the use of multiple views to give different visual representations of a single game "state" at any given moment. As you step through the code, you will gain a better understanding of how these parts encapsulate specific behavior and, at the same time, work together to create a functional application.
Analysis and Design
In any application developed using the MVC pattern, the application code is separated into three parts:
- Model: This part handles and stores all the data. Its responsibility is to do any calculations, parse any data formats, and update itself based on user input. When any data in the model changes, it sends out a notification to the view so that it can update itself.
- View: This part contains the stuff that users see on the screen. The view gives a visual representation of the model's data at any given time. Users interact with the view directly, resulting in potential changes to the system. Also, the view is allowed to call on the model to get information about the current data being stored there.
- Controller: Although it's fairly clear what the model and view do, the role of the controller can be a little fuzzy at times. In AS2MineSweeper, the controller basically initializes both the view and model and then takes user input from the view and updates the model. This gives a layer of abstraction so that the view doesn't directly manipulate the model in any way.
MVC was the underlying framework for how AS2MineSweeper was developed and the layout of the code, from the package structure to the class code, illustrates this (see Figure 1).
Figure 1. This class diagram, created using gModeler (www.gmodeler.com) provides a good idea of exactly how the code is laid out and shows the relationships between classes.
You can see in Figure 1 that there are distinct classes for the model (MinesModel), view (MinesView), and controller (Mines), as well as a few "helper" classes. This code structure made it very easy to modify any given part without causing a ripple effect throughout the system. In fact, the creation of the isometric view was an afterthought and was programmed in about 15 minutes without causing any "blips" in the system.
Installing AS2MineSweeper
The source files consist of one FLA file containing the visual assets and minimal code, along with eight ActionScript class files in a standard package structure. In order to view and modify the FLA file, you must have Macromedia Flash MX 2004 or Flash MX 2004 Professional installed. Although you can view and modify the ActionScript files with any text editor, the ActionScript editor in Flash MX 2004 Professional (or other ActionScript editors) are recommended. One great free editor that supports ActionScript 2.0 is SE|PY (www.sephiroth.it/python/sepy.php).
You should have a basic understanding of how classes and packages work, as well as some understanding of ActionScript 2.0. The code is documented quite thoroughly to help you get around inside it.
Classes
Here is a quick rundown of all the classes and a brief description of what functionality they encapsulate:
- Mines (controller): This is the class that is instantiated to create a new game. It creates instances of the view and model and stores references to them. When a square is clicked in the view (UI), the controller passes that information on to the model to update it.
- MinesModel (model): This class generates and stores all the data associated with the game board. It randomly selects positions for mines, calculates the numbers for all squares adjacent to mines, and responds to clicks from the view (using the controller) in order to update the status of each square.
- Square: This class stores the data associated with a single square. This is only the data; it has nothing to do with the UI.
- MinesView (view): This class encapsulates the code that displays and updates the actual game board, as well as controls and displays elements associated with the game (for example, Switch Views button, Reset button, Time display, Mines Left display).
- Timer: This very minimal class just counts up based on a given interval.
- UISquare: This is the base class for all square movie clips that appear in the UI. It sets the click handler for the square clip and also sets the visual status of the square (such as covered, revealed, flagged, or unknown).
- UISquareNormal: Extending UISquare, this class is the default class for the squares in the UI. This provides the default 2D view. This class is associated with the library item "squareItem" in the FLA file.
- UISquareIsometric: Extending UISquare, this class provides an isometric view of the game board. This class is associated with the library item "squareItemIso" in the FLA file.
Game Parameters
For the most part, the game classes encapsulate all the game and game control functionality. However, there are a few important things to note:
-
You can easily change the parameters of the game by changing the parameters passed to the Mines constructor. The following properties are passed when creating a new game:
- width: number of squares in the width of the game board
- height: number of squares in the height of the game board
- mineNum: number of mines to place on the game board
- viewType: type of view you would like to start with (currently
"normal" or "isometric");
see "Creating New Views" next for information on creating
additional views
- Although you can change these parameters to modify the game board, keep in mind that the current UI "shell" doesn't resize itself, so if you create a large game board, it will likely not fit on the Stage as is. Keep in mind also that the larger the game board, and the more mines you have, the more calculations will be necessary at given points to place mines on the Stage and update the status of multiple squares.
- Currently, the movie clip that contains the game board is created in the MinesView class. It is also placed at a set position on the Stage (using __board._x and __board._y). If you want to change the position of this movie clip, look inside the draw( ) method within MinesView.
Creating New Views
Creating a new view for AS2MineSweeper is fairly painless. Here is all you need to do:
-
Create a new library item with the visual representation of a square that you want in your view. This library item should minimally have states representing the following (the way this is implemented in the two views created with AS2MineSweeper is by using labeled keyframes):
- Default: not clicked yet (covered)
- Empty: revealed but contains no numbers or mines
- Adjacent: revealed and containing a text field to display the number of adjacent mines
- Mine: revealed and containing a mine
- Flag: covered and flagged
- Unknown: covered and tagged with a question mark
- Make sure you export that symbol for ActionScript. Right-click the Library item and select Linkage.
- Create a new class that will be associated with that Library item. It's probably a good idea to extend UISquare but you don't have to. The class needs to know how to change its visual status as well as set up its own click handlers.
- In your FLA file, make sure you associate that class with the Library item you created. Right-click the Library item, select Linkage, and enter the class along with the full class path into the AS 2.0 Class field.
- Go into "com/philterdesign/minesweeper/view/MinesView.as" and
add another condition to the
if..elsestatement in the draw( ) method. This condition should call a method that you create in the next step. - Create a method that will visually position your square symbols (for example, the drawCustomViewItem( ) method). The algorithm to do that is up to you and is based on what the view should look like. If you want a template to start from, look at the drawNormalViewItem( ) method.
That's it. Now when you pass in the name of your new view into the constructor for the controller class it should work. In the current implementation, the Switch View button only toggles between the two views. If you want to change this, you'll have to look at the switchView( ) method.
Next Steps
There's not much else to say other than to encourage you to go and play around in the code. As I mentioned before, it is commented thoroughly so the best way to learn is to read through the code and connect the dots that way. Once you feel comfortable with the code, you might want to change the data source so that it reads its data from XML or a Flash Remoting stream and generate the random mines on the server side. Due to the MVC implementation, it's just a matter of recoding the model and plugging it into the system, and away you go.
Of course, this implementation is based on my take on the MVC design pattern. Other developers may give slightly different perspectives but the overarching concepts will be similar. Fortunately, Flash MX 2004 Professional makes implementation very easy. With the addition of compile-time features like classes, strong typing, private members, and more, it makes implementation and bug catching much quicker than would be possible in the earlier version of Flash MX.